﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.IO;
using IWshRuntimeLibrary;
using myBoard.Framework;

namespace MagnetExtractor
{
    /// <summary>
    /// Interaction logic for Main.xaml
    /// </summary>
    public partial class Main : Window
    {
        private string _appFolder;
        private string _selectedMagnetPath;
        private WshShellClass _wshShell;

        public Main()
        {
            InitializeComponent();
            _appFolder = AppDomain.CurrentDomain.BaseDirectory;
            this.Loaded += new RoutedEventHandler(Main_Loaded);
        }

        void Main_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                OpenFileDialog ofd = new OpenFileDialog();
                string defaultMagnetsPath = System.IO.Path.Combine(System.IO.Path.Combine(Environment.GetEnvironmentVariable("AppData"), "myBoard"), "Magnets");
                ofd.Title = "Select the magnet you want to extract ...";
                ofd.Filter = "Magnet|*.dll";
                if (Directory.Exists(defaultMagnetsPath))
                {
                    ofd.InitialDirectory = defaultMagnetsPath;
                }
                ofd.ShowDialog();


                if (!string.IsNullOrEmpty(ofd.FileName))
                {
                    _selectedMagnetPath = ofd.FileName;
                    string hostsFolder = System.IO.Path.Combine(_appFolder, "MagnetHosts");
                    string magnetHostFolder = System.IO.Path.Combine(hostsFolder,
                        string.Format("{0} - {1}", System.IO.Path.GetFileNameWithoutExtension(ofd.FileName), Guid.NewGuid().ToString()));
                    if (!Directory.Exists(magnetHostFolder))
                        Directory.CreateDirectory(magnetHostFolder);
                    ResourcesAccess.CopyEmbededResourceOnDisk("MagnetHost.exe", magnetHostFolder);
                    ResourcesAccess.CopyEmbededResourceOnDisk("myBoard.Framework.dll", magnetHostFolder);
                    System.IO.File.Copy(ofd.FileName, System.IO.Path.Combine(magnetHostFolder, System.IO.Path.GetFileName(ofd.FileName)));

                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Title = "... and now create a shortcut to launch the magnet.";
                    sfd.FileName = System.IO.Path.GetFileNameWithoutExtension(ofd.FileName).Replace("."," ");
                    sfd.Filter = "Shortcut|*.lnk";
                    sfd.ShowDialog();
                    if (!string.IsNullOrEmpty(sfd.FileName))
                    {
                        CreateShortcut(sfd.FileName, System.IO.Path.Combine(magnetHostFolder, "MagnetHost.exe"));
                        myBoard.Framework.Forms.MessageBox.Show("You can now launch the magnet using this link.","Success !");
                    }
                    this.Close();
                }
                else
                    this.Close();
            }
            catch (Exception ex)
            {
                ExceptionsManager.LogException("Magnet Extractor", ex, true);
                this.Close();
            }
        }
        

        private void CreateShortcut(string shortcutPath, string appPath)
        {
            _wshShell = new WshShellClass();
            IWshRuntimeLibrary.IWshShortcut MyShortcut;
            MyShortcut = (IWshRuntimeLibrary.IWshShortcut)_wshShell.CreateShortcut(shortcutPath);
            MyShortcut.TargetPath = appPath;
            MyShortcut.Description = string.Format("Launch {0}",System.IO.Path.GetFileNameWithoutExtension(_selectedMagnetPath));
            //MyShortcut.IconLocation = Application.StartupPath + @"\app.ico";
            MyShortcut.Save();
        }
    }
}
